package com.sdygmedia.action;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sdygmedia.domain.Admin;
import com.sdygmedia.service.AdminService;

@Controller
public class AdminAction extends ActionSupport {

	private Admin admin;
	@Resource
	private AdminService adminService;

	public String index() throws Exception {
		return "index";
	}
	
	public String list() throws Exception {
		List adminList = adminService.findAll();
		ActionContext.getContext().put("adminList", adminList);
		
		return "list";
	}
	
	public String loginUI() throws Exception {
		return "loginUI";
	}

	public String login() throws Exception {
		//System.out.println(getAdmin());
		ActionContext.getContext().getSession().put("admin", getAdmin());
		if (getAdminService().login(admin.getName(), admin.getPassword())) {
			return "login";
		} else {
			this.addFieldError("loginError", "用户名或密码错误，请重新输入！");
			return "loginUI";
		}
	}
	
	public String addUI() throws Exception {
		return "addUI";
	}
	
	public String add() throws Exception {
		adminService.save(admin);
		
		return "add";
	}
	
	public String delete() throws Exception {
		adminService.delete(admin.getId());
		
		return "toList";
	}
	
	public String updateUI() throws Exception {
		Admin adminUp = adminService.getById(admin.getId());
		ActionContext.getContext().put("adminUp", adminUp);
		return "updateUI";
	}
	
	public String update() throws Exception {
		adminService.update(adminService.getById(admin.getId()));
		
		return "update";
	}

	// ------------------------------------------
	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

}
